package com.gray.wow.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gray.wow.biz.NoticeBiz;
import com.gray.wow.dao.impl.NoticeDaoImpl;
import com.gray.wow.domain.Notice;
import com.gray.wow.domain.PageModel;

@Service("noticeBiz")
@Transactional
public class NoticeBizImpl implements NoticeBiz {
	@Resource
	private NoticeDaoImpl noticeDao;

	@Override
	public PageModel getNotice(Integer pageSize, Integer page) {
		Map<String, Object> map = new HashMap<String, Object>();
		page = page == null ? 1 : page;
		int count = noticeDao.findCountByCondition(map);
		if (pageSize != null) {
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
		}
		List<Notice> list = noticeDao.findListByCondition(map);
		PageModel pm = PageModel.getPageModel(pageSize, page, count, list);
		return pm;
	}

	@Override
	public List<Notice> getNotice(Integer pageSize) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", 0);
		map.put("pageSize", pageSize);
		List<Notice> list = noticeDao.findListByCondition(map);
		return list;
	}

	@Override
	public boolean saveNotice(Notice notice) {
		if (notice != null) {
			if (notice.getId() != null && !notice.getId().equals("")) {
				if (noticeDao.update(notice) == 1)
					return true;
			} else {
				if (noticeDao.add(notice) == 1)
					return true;
			}
		}
		return false;
	}

	@Override
	public boolean delNotice(int... ids) {
		if (ids != null && !ids.equals("")) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ids", ids);
			if (noticeDao.delete(map) == 1)
				return true;
		}
		return false;
	}

	@Override
	public Notice getNoticeById(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<Notice> list = noticeDao.findListByCondition(map);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

}
